package unit1;

import lombok.extern.slf4j.Slf4j;

import java.util.Enumeration;
import java.util.Vector;

/**
 * 顾客
 */
@Slf4j
public class Customer {

    private String name;
    private Vector rentalsVector = new Vector();

    public Customer(String name) {
        this.name = name;
    }

    public void addRental(Rental rental) {
        this.rentalsVector.addElement(rental);
    }

    public String getName() {
        return name;
    }

    /**
     * 用于生成详情单的函数
     *
     * @return
     */
    public String statement() {
        String result = "Rental Record for " + getName() + "\n";
        Enumeration rentals = this.rentalsVector.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += each.getMovie().getTitle() + "\t" + each.getCharge() + "\n";
        }
        result += "Amount owed is " + getTotalCharge() + "\n";
        result += "You earned " + getTotalPoints() + " frequent renter points";
        return result;
    }

    /**
     * 单独计算价格
     *
     * @return
     */
    private int getTotalPoints() {
        int result = 0;
        Enumeration elements = rentalsVector.elements();
        while (elements.hasMoreElements()) {
            Rental each = (Rental) elements.nextElement();
            result += each.getFrequentRenterPoints();
        }
        return result;
    }

    /**
     * 单独计算积分
     *
     * @return
     */
    private double getTotalCharge() {
        double result = 0;
        Enumeration elements = rentalsVector.elements();
        while (elements.hasMoreElements()) {
            Rental each = (Rental) elements.nextElement();
            result += each.getCharge();
        }
        return result;
    }


}
